package com.szm.mvc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

/**
 * @author szm 
 * @ClassName XMLRead.java
 * @Description xml工具类
 *
 */
public class XMLRead {

	/**
	 * @author szm
	 * @Description 根据文件获取Document 
	 * @param file
	 * @return
	 */
	public static Document getDocument(File file) throws Exception{
		InputStream inputStream = null;
		try {
			SAXReader reader = new SAXReader();
			inputStream = new FileInputStream(file);
			Document document = reader.read(inputStream);
			inputStream.close();
			inputStream = null;
			return document;
		} finally {
			if(null != inputStream){
				try {
					inputStream.close();
				} catch (IOException e) {
				}
				inputStream = null;
			}
		}
	}

	/**
	 * @author szm
	 * @Description 根据xml字符串获取Document
	 * @param xmlDoc
	 * @return
	 */
	public static Document getDocument(String xmlDoc) throws Exception{
		StringReader stringReader = new StringReader(xmlDoc);
		InputSource source = new InputSource(stringReader);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(source);
		return document;
	}
	
	/**
	 * @author szm
	 * @Description 根据路径
	 * @param xmlPath
	 * @return
	 * @throws Exception
	 */
	public static Document getDocumentByPath(String xmlPath) throws Exception{
		SAXReader reader = new SAXReader();
		InputStream inputStream = XMLRead.class.getResource("/"+xmlPath).openStream();
		Document document = reader.read(inputStream);
		inputStream.close();
		return document;
	}

	/**
	 * @author szm
	 * @Description Xpath解析 XML
	 * @param document
	 * @param xPath
	 * @return 节点集合
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Node> selectNodes(Document document,String xPath) throws Exception {
		return document.selectNodes(xPath);
	}
	
	/**
	 * @author szm
	 * @Description 遍历节点
	 * @param nodeList
	 * @param nodeName
	 * @return
	 * @throws Exception
	 */
	public static Node selectNode(List<Node> nodeList,String nodeName) throws Exception {
		for (Node node : nodeList) {
			if(nodeName.equals(node.getName())){
				return node;
			}
		}
		return null;
	}
	
	/**
	 * @author szm
	 * @Description 根据document查找
	 * @param document
	 * @param elementName
	 * @return
	 * @throws Exception
	 */
	public static Element getElement(Document document,String elementName) throws Exception {
		Element rootElement = document.getRootElement();
		/*for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			if(elementName.equals(element.getName())){
				return element;
			}
		}*/
		return getElement(rootElement, elementName);
	}
	
	/**
	 * @author szm
	 * @Description 根据根元素查找
	 * @param rootElement
	 * @param elementName
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static Element getElement(Element rootElement,String elementName) throws Exception {
		if(null == rootElement){
			return null;
		}
		for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			if(elementName.equals(element.getName())){
				return element;
			}
		}
		return null;
	}
	
	public static List<Element> getElements(Document document,String elementName) throws Exception {
		Element rootElement = document.getRootElement();
		return getElements(rootElement, elementName);
	}
	
	@SuppressWarnings("rawtypes")
	public static List<Element> getElements(Element rootElement,String elementName) throws Exception {
		if(null == rootElement){
			return null;
		}
		List<Element> list = new ArrayList<Element>();
		for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			if(elementName.equals(element.getName())){
				list.add(element);
			}
		}
		return list;
	}
	
	/**
	 * @author szm
	 * @Description 获取元素值
	 * @param element
	 * @return
	 * @throws Exception
	 */
	public static String getElementValue(Element element) throws Exception {
		if(null == element){
			return null;
		}
		return element.getStringValue();
	}
	
	/**
	 * @author szm
	 * @Description 获取element元素的属性名获取值
	 * @param element 元素
	 * @param attributeName 属性名
	 * @return 属性值
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static String getElementValue(Element element,String attributeName) throws Exception {
		String value = "";
		if(null == element){
			return value;
		}
		for (Iterator i = element.attributeIterator(); i.hasNext();) {
			Attribute attribute = (Attribute) i.next();
			if (attributeName.equals(attribute.getName())) {
				value = attribute.getValue();
			}
		}
		return value;
	}
	
}
